package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.entity.WmNews;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import com.heima.media.service.IWmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {
    @Override
    public ResponseResult listByName(ChannelDto dto) {
        // 模拟异常
        // int i = 1 / 0;
        // 分页查询
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        // 构建查询条件
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        // 判断dto中的name是否为空,不为空添加查询条件
        if (!StringUtils.isEmpty(dto.getName())) {
            query.like(WmChannel::getName, dto.getName());
        }
        IPage<WmChannel> iPage = this.page(page, query);
        // 构建通用分页响应
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(),
                iPage.getTotal(), iPage.getRecords());
        return result;
    }

    @Override
    public ResponseResult saveChannel(WmChannel channel) {
        // 判断dto对象是否为空  判断传入的频道名称是否为空
        if (channel == null || StringUtils.isEmpty(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 判断频道名称是否已经存在
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName, channel.getName());
        WmChannel one = this.getOne(query);
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 不存在,新增频道
        channel.setCreatedTime(new Date());
        this.save(channel);
        return ResponseResult.okResult(channel.getId());
    }

    @Autowired
    private IWmNewsService newsService;

    @Override
    public ResponseResult updateChannel(WmChannel channel) {
        if (channel == null || StringUtils.isEmpty(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 修改的频道名称不能与数据库中其他频道重复
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName, channel.getName());
        query.ne(WmChannel::getId, channel.getId());
        WmChannel one = this.getOne(query);
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 如果频道被使用中,不能禁用
        if (!channel.getStatus()) {
            LambdaQueryWrapper<WmNews> queryNews = new LambdaQueryWrapper<>();
            queryNews.eq(WmNews::getChannelId, channel.getId());
            int count = newsService.count(queryNews);
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CAN_NOT_UPDATE);
            }
        }
        this.updateById(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {
        // 根据id查询频道
        WmChannel channel = this.getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 只有禁用的频道才能删除
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CAN_NOT_DELETE);
        }
        this.removeById(id);
        return ResponseResult.okResult();
    }
}
